﻿using System;
using System.Globalization;
using System.Windows.Controls;

namespace XamlTorrent.Common.Validation
{
    public class UriValidationRule : ValidationRule
    {
        public UriValidationRule()
        {
        }

        private UriKind uriKind;

        public UriKind UriKind
        {
            get { return uriKind; }
            set { uriKind = value; }
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            System.Diagnostics.Trace.WriteLine(
                String.Format("UriValidationRule.Validate(\"{0}\", {1})", value, cultureInfo)
                );

            //try
            //{
            //    new Uri((string)value, uriKind);
            //    return new ValidationResult(true, null);
            //}
            //catch (UriFormatException ex)
            //{
            //    return new ValidationResult(false, ex);
            //}

            Uri dummy;
            if (Uri.TryCreate((string)value, uriKind, out dummy))
                return new ValidationResult(true, null);
            else
                return new ValidationResult(false, "Not an absolute URI.");
        }
    }
}
